package com.huawei.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huawei.crm.domain.Department;
import com.huawei.crm.mapper.DepartmentMapper;
import com.huawei.crm.query.QueryObject;
import com.huawei.crm.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DepartmentServiceImpl extends AbstractService<DepartmentMapper, Department, Long>
        implements IDepartmentService {

    @Autowired
    private DepartmentMapper mapper;

    @Override
    public void saveOrUpdateD(Department d) {
        if (d.getId() == null) {
            mapper.insert(d);
        } else {
            mapper.updateByPrimaryKey(d);
        }
    }

    @Override
    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = true)
    public Department get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Department> listAll() {
        return mapper.selectAll();
    }

    @Override
    @Transactional(readOnly = true)
    public PageInfo<Department> query(QueryObject qo) {

        // 告诉mybatis需要去哪里拼接sql
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Department> list = mapper.selectForList(qo);
        return new PageInfo<>(list);
    }
}


